﻿<?php
class M_account extends CI_Model{
	 function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }





function login($username)
    {
        $data = array('username'=>$username, 'logged_in'=>TRUE);
        $this->session->set_userdata($data);                    //添加session数据
        }
    /**
     * 通过用户名获得用户记录
     * @param string $username
     */
    function get_by_username($username)
    {
        $this->db->where('username', $username);
        $query = $this->db->get('account');
        //return $query->row();                            //不判断获得什么直接返回
        if ($query->num_rows() == 1)
        {
             return $query->row();
        }
        else
        {
            return FALSE;
        }
    }
    


    	function a($a){
		   
		}


    /**
     * 用户名不存在时,返回false
     * 用户名存在时，验证密码是否正确
     */
    function password_check($username, $password)
    {                
        if($user = $this->get_by_username($username))
        {
            return $user->password == $password ? TRUE : FALSE;
        }
        return FALSE;                                    //当用户名不存在时
    }



  function logout()
    {

    	if (isset($_SESSION["logged_in"])) {
    		     if ($_SESSION["logged_in"]=== TRUE)
		        {
		            $this->session->sess_destroy();                //销毁所有session的数据
		            return TRUE;
		        }
		        return FALSE;
    	}
    
    }






	//得到一个符合主键的信息
	function get1($a){
   
	 $this->db->where("id",$a);

	$query=$this->db->get("account");

	return $query->result();
	}


  	function read($num,$offset){

	//$this->db->order_by('id DESC, name ASC');
	$this->db->order_by('id DESC');
	$query=$this->db->get("account",$num,$offset);
	return $query->result();
	}


 
	


	function insert(){
	$username = $this->input->post("username",true);
	$password = md5($this->input->post("password",true));
	$email = $this->input->post("email",true);
	$data=array("username" => $username,"password" => $password,"email" => $email );
	$this->db->insert("account",$data);
	$this->login($username);
	}


	function update($a){
	$username = $this->input->post("username",true);
$password = $this->input->post("password",true);
$email = $this->input->post("email",true);
$data=array("username" => $username,"password" => $password,"email" => $email );
	$this->db->where("id",$a);
	$this->db->update("account",$data);
	}


	function delete($a){
	$this->db->delete($a);
	}





}